/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrpe.bsi.manegedbean;

import br.ufrpe.bsi.bean.Compromisso;
import br.ufrpe.bsi.bean.Log;
import br.ufrpe.bsi.dao.CompromissoDAO;
import br.ufrpe.bsi.dao.LogDAO;
import br.ufrpe.bsi.dao.UsuarioDAO;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Daniel_Barreto
 */
public class CompromissoManagedBean {
    private CompromissoDAO compromissoDAO = new CompromissoDAO();
    private Compromisso compromisso = new Compromisso(); 
    LogDAO logDAO = new LogDAO(); 
    private List<Compromisso> compromissos;
    
    public CompromissoManagedBean() throws ClassNotFoundException, SQLException{
    }

    public String cadastrarCompromisso(){
        try {
            if(compromisso.validarCompromisso().equals("erroDatas")){
                return "erroDatas";
            }
            UsuarioDAO usuarioDAO = new UsuarioDAO();
            String login = LoginManegedBean.recuperarLogin();
            compromisso.setUsuario(usuarioDAO.retornar(login));
            compromissoDAO.inserir(compromisso);
            Log log = new Log("Cadastrar Compromisso"+compromisso.toString());
            logDAO.inserir(log);
            return "listandoCompromissos";
        } catch(Exception ex){
            return "erroGeral";
        }
    }
    
    public List<Compromisso> getCompromissos() throws ClassNotFoundException, SQLException{      
        String login = LoginManegedBean.recuperarLogin();
        compromissos = compromissoDAO.retornarCompromissos(login);
        return compromissos;
    }
  
    public Compromisso getCompromisso() {
        return compromisso;
    }

    public void setCompromisso(Compromisso compromisso) {
        this.compromisso = compromisso;
    }
    
    public String inicioEditarCompromisso(){
        return "vaParaEditar";
    }
    
    public String fimEditarCompromisso(){
        if(compromisso.validarCompromisso().equals("erroDatas")){
            return "erroDatas";
        }
        try {
            compromissoDAO.editar(compromisso);
            Log log = new Log("Editar compromisso"+compromisso.toString());
            logDAO.inserir(log);
            return "listandoCompromissos";
        }catch(Exception ex){
            return "erroGeral";
        }
    }
    
    public String excluirCompromisso(){
        try {
            compromissoDAO.excluir(compromisso);
            Log log = new Log("Excluir compromisso"+compromisso.toString());
            System.out.println(log.getAcao());
            logDAO.inserir(log);
            return "listandoCompromissos";
        }catch(Exception ex){
            return "erroGeral";
        }
    }
}